package hivi.vn.hivigold.fragment;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Item;
import hivi.vn.hivigold.utils.Formats;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

public class SearchItemFragment extends Fragment {
	final int SCAN_REQUEST = 1;
	final int SHOW_TOAST = 1;
	final int SHOW_ITEM = 2;

	Button btnScan;
	Button btnSearch;
	Button btnBuy;
	SearchView sv;

	TextView tvItemName;
	TextView tvItemGroup;
	TextView tvTlgau;
	TextView tvTlgda;
	TextView tvTongtlg;
	TextView tvNote;
	TextView tvSalary;
	TextView tvStone;
	TextView tvGoldPrice;
	TextView tvPrice;
	TextView tvHlgau;
	Item selected;
	SearchListener sl;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.searchitem_layout, null);

		// Controls
		btnScan = (Button) v.findViewById(R.id.btnScan);
		btnSearch = (Button) v.findViewById(R.id.btnSearch);
		btnBuy = (Button) v.findViewById(R.id.btnBuy);
		sv = (SearchView) v.findViewById(R.id.txtSearchItem);
		tvItemName = (TextView) v.findViewById(R.id.tvItemName);
		tvItemGroup = (TextView) v.findViewById(R.id.tvGroupItem);
		tvTlgau = (TextView) v.findViewById(R.id.tvTlg_au);
		tvTlgda = (TextView) v.findViewById(R.id.tvTlg_da);
		tvTongtlg = (TextView) v.findViewById(R.id.tvTongtlg);
		tvNote = (TextView) v.findViewById(R.id.tvItemNote);
		tvSalary = (TextView) v.findViewById(R.id.tvSalary);
		tvStone = (TextView) v.findViewById(R.id.tvMoneyStone);
		tvGoldPrice = (TextView) v.findViewById(R.id.tvGoldPrice);
		tvPrice = (TextView) v.findViewById(R.id.tvPriceBuy);
		tvHlgau = (TextView) v.findViewById(R.id.tvHlg_au);

		// Set event
		btnScan.setOnClickListener(onClick);
		btnBuy.setOnClickListener(onClick);
		btnSearch.setOnClickListener(onClick);
		getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		return v;
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnScan) {
				Intent i = new Intent("com.google.zxing.client.android.SCAN");
				i.putExtra("SCAN_MODE", "CODE_128");
				startActivityForResult(i, SCAN_REQUEST);
			} else if (v.getId() == R.id.btnBuy) {
				if(selected==null)
				{
					Toast.makeText(getActivity(), R.string.warning_sale_nothing, Toast.LENGTH_LONG).show();
					return;
				}
				if(sl!=null)
				{
					sl.saleClickListener(selected);
				}
			} else if (v.getId() == R.id.btnSearch) {
				hideKeyboard();
				String key = sv.getQuery().toString().trim();
				if (!key.equals("")) {
					ItemAsync itemAsync = new ItemAsync();
					itemAsync.execute(key);
				}
			}
		}
	};
	void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.hideSoftInputFromWindow(sv.getWindowToken(), 0);
	}
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == SCAN_REQUEST) {
				sv.setIconified(false);
				sv.setQuery(data.getStringExtra("SCAN_RESULT"), true);
				String key = sv.getQuery().toString().trim();
				if (!key.equals("")) {
					ItemAsync itemAsync = new ItemAsync();
					itemAsync.execute(key);
				}
				/* getItem(); */
			}
		}
	};

	private class ItemAsync extends AsyncTask<String, Void, Item> {
		ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(getActivity());
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}

		@Override
		protected void onPostExecute(Item result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			selected = result;
			dialog.dismiss();
			if(result!=null)
			{
				tvItemName.setText(result.getVtName());
				tvItemGroup.setText(result.getFK_Nhvt2ID());
				tvTlgau.setText(Formats.number(result.getTlg_au()));
				tvTlgda.setText(Formats.number(result.getTlg_da()));
				tvTongtlg.setText(Formats.number(result.getTong_tlg()));
				tvSalary.setText(Formats.money(result.getTien_cong()));
				tvStone.setText(Formats.money(result.getTien_da()));
				tvGoldPrice.setText(Formats.money(result.getGia_au()));
				tvPrice.setText(Formats.money(result.getGia_ban11()));
				tvNote.setText(result.getDienGiai());
				tvHlgau.setText(Formats.percent(result.getHlg_au()));	
			}
			else
			{
				Toast.makeText(getActivity(), R.string.alert_item_not_exist,
						Toast.LENGTH_LONG).show();
			}
			hideKeyboard();
		}

		@Override
		protected Item doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			Item i =WebService.getItemByID(getActivity(), arg0[0], "GIABAN");
			return i;
		}

	}
	public void setListener(SearchListener sl)
	{
		this.sl = sl;
	}
	
	public interface SearchListener
	{
		public void saleClickListener(Item i);
	}
}
